package com.hsmpay.mobile.action.other;

import com.hsmpay.common.pojo.user.User;
import com.hsmpay.common.util.URLDecoderTools;
import com.hsmpay.mobile.action.base.MobileBaseAction;
import com.hsmpay.mobile.pojo.other.Feedback;
import com.hsmpay.mobile.service.other.FeedbackService;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户反馈 action
 *
 * @author 颜铃璋
 * @date 2013-1-18
 */
@Controller("feedbackAction")
@Scope("prototype")
public class FeedbackAction extends MobileBaseAction {
    private static final long serialVersionUID = -337060378804571678L;
    private static Logger log = Logger.getLogger(FeedbackAction.class);
    @Resource(name = "feedbackService")
    private FeedbackService<Feedback, Long> feedbackService;

    private String text;//用户反馈信息


    /**
     * 意见反馈
     *
     * @return
     */
    public String userFeedback() throws Exception {
        log.debug("--- userFeedback() start ---");
        JSONObject js = JSONObject.fromObject(getRequest().getAttribute(FC_DATA));
        terminalTypeId = Long.parseLong(js.getString("terminalTypeId"));
        clientType = Integer.parseInt(js.getString("clientType"));
        roleId = Long.parseLong(js.getString("roleId"));
        if (js.containsKey("userId"))
            userId = Long.parseLong(js.getString("userId"));
        if (js.containsKey("text"))
            text = js.getString("text");
        log.debug("terminalTypeId = " + terminalTypeId);
        log.debug("roleId = " + roleId);
        log.debug("clientType = " + clientType);
        log.debug("userId = " + userId);
        log.debug("text = " + text);
        if (null == userId) {
            log.warn("意见反馈时, 传入参数userId为空");
            sendAppErrorMsg("6001", "userId");
            return null;
        }
        if (StringUtils.isBlank(text)) {
            log.warn("意见反馈时, 传入参数text为空");
            sendAppErrorMsg("6001", "text");
            return null;
        }

        try {
            text = URLDecoderTools.decodeUTF8(text);
            User user = userService.getUserById(userId);
            if (null != user) {
                Feedback feedback = new Feedback();
                feedback.setTerminalTypeId(terminalTypeId);
                feedback.setRoleId(roleId);
                feedback.setClientType(clientType);
                feedback.setUserId(userId);
                feedback.setText(text);
                feedback.setOrganizationId(user.getOrganizationId());
                long flag = feedbackService.insertFeedback(feedback);
                if (flag > 0) {
                    log.debug("意见反馈提交成功");
                    Map<String, Object> result = new HashMap<String, Object>();
                    result.put("reqCode", "0000");
                    result.put("reqMsg", "意见反馈提交成功！");
                    sendAppMsg(result);
                } else {
                    log.warn("意见反馈数据库处理失败！");
                    sendAppErrorMsg("9999");
                }
            } else {
                log.warn("用户反馈时, 用户Id=" + userId + " 对应的用户不存在!");
                sendAppErrorMsg("6012", userId + "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            log.debug("--- userFeedback() end ---");
        }
        return null;
    }
}
